package com.example.userdemo.handler;

import com.alibaba.fastjson.JSON;
import com.example.userdemo.entity.Trace;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * 统一记录用户操作日志
 */


//@Component
//@Aspect
@Slf4j
public class ServiceLogHandler {

    @Pointcut("execution(* com.example.userdemo.*.*.*(..))")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        Trace trace = new Trace();
        // 用户[1.2.3.4],在[xxx],访问了[com.example.userdemo.service.xxx()].
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        String ip = request.getRemoteHost();
        trace.setIp(ip);
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        trace.setTime(LocalDateTime.now());
        String target =joinPoint.getSignature().getDeclaringTypeName() + '.'+ joinPoint.getSignature().getName();
        trace.setTarget(target);
//        log.info(String.format("用户[%s],在[%s],访问了[%s].", ip, now, target));
        trace.setXtraceid("02bf6e6e-c731-4ea5-a306-78b51039e199");
        String string_trace = JSON.toJSONString(trace);
        log.info(string_trace);
    }

//    @Around("pointcut()")
//    public Object handlerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//        Trace trace = new Trace();
//
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if (Objects.isNull(attributes)){
//            return null;
//        }
//        HttpServletRequest request = attributes.getRequest();
//        String ip = request.getRemoteHost();
//        trace.setIp(ip);
//        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//        trace.setTime(LocalDateTime.now());
//        String target =proceedingJoinPoint.getSignature().getDeclaringTypeName() + '.'+ proceedingJoinPoint.getSignature().getName();
//        trace.setTarget(target);
//        trace.setXtraceid("02bf6e6e-c731-4ea5-a306-78b51039e199");
//        String string_trace = JSON.toJSONString(trace);
//        return proceedingJoinPoint.proceed();
//    }

//    @After("pointcut()")
//    public void after(JoinPoint joinPoint){
//        System.out.println("切点之后");
//    }
}
